package com.jader.activities;
//订单详情
import com.jader.beans.Address;
import com.jader.beans.OrderDetail;
import com.jader.fragment.Order_detail_bottom_fragment;
import com.jader.fragment.Order_detail_bottom_fragment_BUY;
import com.jader.myviews.PaymentPopWindowTWO;
import com.jader.myviews.PingJiaPopWindow;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.Intent;

public class OrderDetailActivity extends Activity implements OnClickListener {

	private TextView order_save_username, order_save_userphone,
			order_save_address, order_sellername, order_goods_name,
			order_goods_price, order_goods_desc, order_goods_number,
			order_count_price,confirm_order;
	private ImageView order_goods_pic;
	private FragmentManager manager;
	private OrderDetail orderDetail;
	private RelativeLayout relative_order_two;
	private PaymentPopWindowTWO popWindow;
	private PingJiaPopWindow popWindow2;
	private View yingyin_two;
	private HttpUtils httpUtils;
	private String url = JaderApplication.getIP() + "/Jader/client";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_detail);
		initViews();
		initData();

	}

	private void initViews() {
		manager = getFragmentManager();
		order_save_username = (TextView) findViewById(R.id.order_save_username);
		order_save_userphone = (TextView) findViewById(R.id.order_save_userphone);
		order_save_address = (TextView) findViewById(R.id.order_save_address);
		order_sellername = (TextView) findViewById(R.id.order_sellername);
		order_goods_name = (TextView) findViewById(R.id.order_goods_name);
		order_goods_price = (TextView) findViewById(R.id.order_goods_price);
		order_goods_desc = (TextView) findViewById(R.id.order_goods_desc);
		order_goods_number = (TextView) findViewById(R.id.order_goods_number);
		order_count_price = (TextView) findViewById(R.id.order_count_price);
		order_goods_pic = (ImageView) findViewById(R.id.order_goods_pic);
		relative_order_two = (RelativeLayout) findViewById(R.id.relative_order_two);
		yingyin_two = findViewById(R.id.yingyin_two);
		confirm_order = (TextView) findViewById(R.id.confirm_order);
		popWindow = new PaymentPopWindowTWO(this);
		popWindow2 = new PingJiaPopWindow(this);
		confirm_order.setOnClickListener(this);
		httpUtils = new HttpUtils();
		popWindow
				.setOnItemClickListener(new PaymentPopWindowTWO.OnItemClickListener() {

					@Override
					public void onClickOKPop() {
						setBackgroundBlack(yingyin_two, 1);

					}
				});
		popWindow2
				.setOnItemClickListener(new PingJiaPopWindow.OnItemClickListener() {

					@Override
					public void onClickOKPop() {
						setBackgroundBlack(yingyin_two, 1);

					}
				});
	}

	private void initData() {
		orderDetail = (OrderDetail) getIntent().getSerializableExtra("order");
		if ("1".equals(orderDetail.getStatus())) {
			replace(1);
		} else {
			replace(2);
		}
		Address address = orderDetail.getAddress();
		double price = orderDetail.getGoods().getPrice();
		int number = orderDetail.getNumber();
		order_save_username.setText(address.getReceiver());
		order_save_userphone.setText(address.getPhone());
		order_save_address.setText(address.getProvince() + address.getCity()
				+ address.getArea() + address.getDetail());
		order_goods_price.setText("￥" + price);
		order_goods_desc.setText(orderDetail.getGoods().getDesc());
		order_goods_number.setText("" + number);
		order_sellername.setText("   "
				+ orderDetail.getSeller().getSellername() + "   ");
		order_goods_name.setText(orderDetail.getGoods().getGoodsName());
		order_count_price.setText("￥"
				+ JaderApplication.getPrice(price * number));
		JaderApplication.getBitmapUtils().display(order_goods_pic,
				JaderApplication.getIP()+"/Jader" + orderDetail.getGoods().getPicture());
		order_sellername.setOnClickListener(this);
		relative_order_two.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.order_sellername:
			Intent intent1 = new Intent(this, Store.class);
			intent1.putExtra("sellerid", orderDetail.getSeller().getSellerid());
			startActivity(intent1);
			break;
		case R.id.relative_order_two:
			Intent intent2 = new Intent(this, Seller.class);
			intent2.putExtra("goods", orderDetail.getGoods());
			startActivity(intent2);
			break;
		case R.id.order_detail_pay:
			popWindow.payment(orderDetail);
			setBackgroundBlack(yingyin_two, 0);
			popWindow.showAsDropDown(arg0);

			break;
		case R.id.order_detail_phone:
			Intent phone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ orderDetail.getSeller().getPhone()));
			startActivity(phone);
			break;
		case R.id.confirm_order:
			finish();
			break;
		case R.id.order_detail_delete_order:
			new AlertDialog.Builder(this)
					.setTitle("您确定要取消订单吗？")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									RequestParams params = new RequestParams();
									params.addBodyParameter(
											"type",
											JaderApplication.DELETE_GOODS_ORDER_CANCEL
													+ "");
									params.addBodyParameter("orderid",
											orderDetail.getOrderid() + "");
									sendRequest(url, params);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {

								}
							}).show();

			break;
		case R.id.order_detail_pay_buy:
			// 评价已购买的订单
			popWindow2.setGoods(orderDetail.getGoods());
			setBackgroundBlack(yingyin_two, 0);
			popWindow2.showAsDropDown(arg0);
			break;
		case R.id.order_detail_phone_buy:
			Intent phone1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
					+ orderDetail.getSeller().getPhone()));
			startActivity(phone1);
			break;
		case R.id.order_detail_delete_order_buy:
			new AlertDialog.Builder(this)
					.setTitle("您确定要删除订单吗？")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									RequestParams params = new RequestParams();
									params.addBodyParameter(
											"type",
											JaderApplication.DELETE_GOODS_ORDER_CANCEL
													+ "");
									params.addBodyParameter("orderid",
											orderDetail.getOrderid() + "");
									sendRequest(url, params);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {

								}
							}).show();

			break;

		default:
			break;
		}
	}

	public void setBackgroundBlack(View view, int what) {
		switch (what) {
		case 0:
			view.setVisibility(View.VISIBLE);
			break;
		case 1:
			view.setVisibility(View.GONE);
			break;
		}
	}

	private void sendRequest(String url, RequestParams params) {

		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						JaderApplication.show(OrderDetailActivity.this,
								"操作失败，请重试！");
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String result = arg0.result;
						if (!JaderApplication.isEmpty(result)
								&& "true".equals(result)) {
							finish();
						} else {
							JaderApplication.show(OrderDetailActivity.this,
									"操作失败，请重试！");

						}

					}
				});
	}

	public RequestParams pay() {
		RequestParams params = new RequestParams();
		params.addBodyParameter("type",
				JaderApplication.DELETE_GOODS_ORDER_CANCEL + "");
		params.addBodyParameter("orderid", orderDetail.getOrderid() + "");
		return params;
	}

	public void replace(int flag) {
		switch (flag) {
		case 1:
			manager.beginTransaction()
					.replace(R.id.order_detail_bottom,
							new Order_detail_bottom_fragment()).commit();
			break;
		case 2:
			manager.beginTransaction()
					.replace(R.id.order_detail_bottom,
							new Order_detail_bottom_fragment_BUY()).commit();
			break;
		default:
			break;
		}
	}
}
